package com.lz.yz.member.web;

import com.github.pagehelper.PageHelper;
import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.base.util.DateUtil;
import com.lz.yz.home.entity.Domain;
import com.lz.yz.member.service.PropertyInfoService;
import com.lz.yz.order.entity.Turnovers;
import com.lz.yz.order.service.TurnoversService;
import com.util.common.Page;
import com.util.common.QueryList;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* Created by CodeGenerator on 2017/10/11.
*/
@RestController
@RequestMapping("/turnovers")
public class PropertyInfoController {

    @Autowired
    HttpServletRequest request;

    @Autowired
    TurnoversService turnoversService;


    @ApiOperation(value="流水列表",response = Turnovers.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "Authorization",value = "Bearer token")
    })
    @GetMapping
    public ReturnValue list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        Integer accountId = (Integer) request.getAttribute("accountId");
        Condition condition = new Condition(Turnovers.class);
        Example.Criteria criteria = condition.or();
        criteria.andEqualTo("accountId", accountId);
        condition.setOrderByClause("created_at desc");
        List<Turnovers> turnovers = turnoversService.findByCondition(condition);
        turnovers.forEach(t -> {
            t.setCreatedAtDesc(DateUtil.format(t.getCreatedAt(),DateUtil.defaultYmdhmPattern));
        });
        QueryList<Domain> queryList = new QueryList(turnovers, Page.toPage(turnovers));
        return ReturnValue.generateTrueReturnValue(queryList);
    }

}
